package com.sm.controller;

import cn.hutool.core.util.StrUtil;
import com.sm.model.HappyRequest;
import com.sm.model.HappySolve;
import com.sm.service.HappyRequestService;
import com.sm.service.HappySolveService;
import com.sm.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.Date;

@Controller
@RequestMapping("/happySolve")
public class HappySolveController {

    @Autowired
    private HappySolveService happySolveService;

    @Autowired
    private HappyRequestService happyRequestService;
    /**
     * 幸福问答回答接口(未使用)
     * @param requestIdStr
     * @param solveUserStr
     * @param content
     * @return
     */
    @ResponseBody
    @PostMapping("/insertSolve")
    public Msg insertSolve(@RequestParam("requestId")String requestIdStr,@RequestParam("solveUser")String solveUserStr,@RequestParam("content")String content){
        Integer solveUser = null;
        Integer requestId = null;
        if (!ObjectUtils.isEmpty(solveUserStr)){
            solveUser=Integer.valueOf(solveUserStr);
        }
        if (!ObjectUtils.isEmpty(requestIdStr)){
            requestId = Integer.valueOf(requestIdStr);
        }
        if (StrUtil.isBlank(content)) {
            return Msg.success().add("msg","请填写内容");
        }
        HappySolve happySolve = new HappySolve();
        happySolve.setContent(content);
        happySolve.setSolveUser(solveUser);
        happySolve.setCreateTime(new Date());
        happySolve.setRequestId(requestId);
        updateCommentNum(requestId);
        int i = happySolveService.insertSolve(happySolve);
        if (i == 1){
            return Msg.success().add("msg","幸福问答回答成功");
        }
        return Msg.fail().add("msg","回答失败");
    }


    //将幸福问答的问题评论次数+1
    public void updateCommentNum(Integer requestId){
        HappyRequest happyRequest = happyRequestService.selectRequestById(requestId);
        HappyRequest request = new HappyRequest();
        request.setCommentNum(happyRequest.getCommentNum()+1);
        request.setRequestId(requestId);
        happyRequestService.update(request);
    }
}
